package com.zzyq.pc.utils;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.lang3.StringUtils;

public class ReportUtils {

	/**
	 * 正则替换字符串中的元素
	 */
	public static String getReplaceAll(String input, String regex, String replacement) {
		if (input == null)
			return null;
		return Pattern.compile(regex).matcher(input).replaceAll(replacement);
	}

	/**
	 * 获取图像流的维度信息
	 */
	public static Dimension getImgDimension(InputStream inputStream) {
		if (inputStream == null) {
			return null;
		}
		BufferedImage image = null;
		try {
			image = ImageIO.read(inputStream);
			if (image == null) {
				return null;
			}
			return restrictMaxSize(new Dimension(image.getWidth(), image.getHeight()));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.reset();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 限制图片的最大大小
	 */
	private static Dimension restrictMaxSize(Dimension size) {
		int width = size.width;
		int height = size.height;
		if (width > 425) {
			height = height * 425 / width;
			width = 425;
			size.height = height;
			size.width = width;
		}
		return size;
	}

	/**
	 * 将svgcode字符串转为png,并以输入流InputStream的形式返回，以便写入文档
	 */
	public static InputStream convertToPng(String svgCode) {
		return new ByteArrayInputStream(convertToPngByte(svgCode));
	}

	public static byte[] convertToPngByte(String svgCode) {
		if (StringUtils.isBlank(svgCode)) {
			return null;
		}
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			byte[] bytes = svgCode.getBytes("UTF-8");
			PNGTranscoder t = new PNGTranscoder();
			TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
			TranscoderOutput output = new TranscoderOutput(outputStream);
			t.transcode(input, output);
			outputStream.flush();
			return outputStream.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {

		File file = new File(pngFilePath);

		FileOutputStream outputStream = null;
		try {
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			convertToPng(svgCode, outputStream);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
		try {
			byte[] bytes = svgCode.getBytes("UTF-8");
			PNGTranscoder t = new PNGTranscoder();
			TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
			TranscoderOutput output = new TranscoderOutput(outputStream);
			t.transcode(input, output);
			outputStream.flush();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
